/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package logic.services;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import edu.fca.beans.CalendarioBean;
import edu.fca.daos.CalendarioDAO;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import logic.services.exceptions.InvalidDateException;
import logic.services.exceptions.MaxNumberOfCalendarsException;

/**
 *
 * @author Romms
 */
public class CalendarioServices {
    private static CalendarioDAO calendarioDAO = new CalendarioDAO();

    private CalendarioServices(){
    }

    private static CalendarioServices instance;

    public static CalendarioServices getInstance(){
        if(instance==null){
            instance= new CalendarioServices();
        }
        return instance;

    }

    public ArrayList <CalendarioBean> getAllValidCalendars(){
        try {
            Calendar cal = Calendar.getInstance();
            return calendarioDAO.selectWhere("fecha>"+ "'" +new java.sql.Date(cal.getTime().getTime())+"'");
        } catch (Exception ex) {
            Logger.getLogger(CalendarioServices.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public void addCalendario(CalendarioBean calendarioBean) throws MySQLIntegrityConstraintViolationException,MaxNumberOfCalendarsException, InvalidDateException{
        try {
            verifyInfo(calendarioBean);
            calendarioDAO.add(calendarioBean);
        }catch (InvalidDateException ex) {
            throw ex;
        } catch (MySQLIntegrityConstraintViolationException ex) {
            throw ex;
        }catch (MaxNumberOfCalendarsException ex) {
            throw ex;
        }catch (Exception ex) {
            Logger.getLogger(CalendarioServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void updateCalendario(CalendarioBean calendarioNuevo, String codigo){
        try {
            calendarioDAO.updateWhere(calendarioNuevo, "codigo='" + codigo + "'");
        } catch (Exception ex) {
            Logger.getLogger(CalendarioServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void deleteCalendario(CalendarioBean calendarioBean){
        try {
            calendarioDAO.deleteWhere("codigo='" + calendarioBean.getCodigo() + "'");
        } catch (Exception ex) {
            Logger.getLogger(CalendarioServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void verifyInfo(CalendarioBean calendarioBean) throws InvalidDateException, MaxNumberOfCalendarsException{
        if(!dateIsValid(calendarioBean.getFecha())){
                throw new InvalidDateException("La fecha seleccionada es menor o igual a la de hoy.");
        }
        if(calendarioDAO.getCountOfAdminCalen(calendarioBean.getProfesorCodigo())==4){
            throw new MaxNumberOfCalendarsException("El número de calendarios asignados al profesor " +
                    "se ha excedio, elija otro profesor.");
        }
        
    }

    private boolean dateIsValid(Date date){
        Calendar cal = new GregorianCalendar() {};
        Date currentDay=new  java.sql.Date( cal.getTime().getTime() );

        if(currentDay.compareTo(date)<0)
            return true;
        return false;
    }

}
